﻿using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using SER = CPPEI.ETL.Domain.Services;
using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using VIW = CPPEI.ETL.Domain.Views;

namespace CPPEI.ETL.WebApi
{
    /// <summary>
    /// 获取数据源的参数列表
    /// </summary>
    [CPPEI.WebApi.AuthGetAction("ETL", "GetDataSourceParameterList", "1.0")]
    public class GetDataSourceParameterListAction : BaseAction<GetDataSourceParameterListParams, GetDataSourceParameterListResult>
    {
        protected override ActionResult<GetDataSourceParameterListResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            GetDataSourceParameterListParams opParams)
        {
            SER.DtSerService ser = new SER.DtSerService();

            ListResult<ENT.EDtSourceParameter> serResult = ser.GetDtSourceParameters(opParams.DataServiceId, opParams.DataSourceId);

            if (serResult.IsSuccess)
            {
                GetDataSourceParameterListResult result = new GetDataSourceParameterListResult();

                foreach (var model in serResult.Data)
                {
                    GetDataSourceParameterListResult.Item item = new GetDataSourceParameterListResult.Item();
                    item.Id = model.Id;
                    item.Name = model.Name;
                    item.Type = model.DataType;
                    item.Index = model.Index;

                    result.Items.Add(item);
                }

                return new ActionResult<GetDataSourceParameterListResult>(result);

            }
            return new ActionResult<GetDataSourceParameterListResult>(false, serResult.ErrorCode, serResult.Message, null);
        }
    }

    public class GetDataSourceParameterListParams
    {
        [Parameter]
        public Guid DataServiceId { get; set; }

        [Parameter]
        public Guid DataSourceId { get; set; }
    }

    public class GetDataSourceParameterListResult
    {
        public List<Item> Items { get; set; }

        public GetDataSourceParameterListResult()
        {
            Items = new List<Item>();
        }

        public class Item
        {
            public Guid Id { get; set; }

            public string Name { get; set; }

            public string Type { get; set; }

            public int Index { get; set; }
        }
    }
}
